/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.lucene.service.SearchService;
import com.youth.fund.hk.hostManager.manager.dao.HtmlMapperDao;
import com.youth.fund.hk.hostManager.manager.datamodel.CaseModel;
import com.youth.fund.hk.hostManager.manager.datamodel.HtmlMapperModel;
import com.youth.fund.hk.hostManager.manager.datamodel.InformationModel;
import com.youth.fund.hk.hostManager.manager.datamodel.NewsModel;
import com.youth.fund.hk.hostManager.manager.datamodel.ProductModel;
import com.youth.fund.hk.hostManager.manager.datamodel.RecruitmentModel;
import com.youth.fund.hk.hostManager.manager.datamodel.HtmlMapperModel.HtmlType;
import com.youth.fund.hk.hostManager.manager.datamodel.RelatedProductModel;
import com.youth.fund.hk.hostManager.manager.datamodel.TypeModel.TypeModelType;
import com.youth.fund.hk.hostManager.manager.datamodel.TypeModel;
import com.youth.fund.hk.hostManager.manager.service.CaseService;
import com.youth.fund.hk.hostManager.manager.service.InitService;
import com.youth.fund.hk.hostManager.manager.service.NewsService;
import com.youth.fund.hk.hostManager.manager.service.ProductService;
import com.youth.fund.hk.hostManager.manager.service.RecruitmentService;
import com.youth.fund.hk.hostManager.manager.service.RelatedProductService;
import com.youth.fund.hk.hostManager.manager.service.TypeService;
import com.youth.fund.hk.hostManager.thymeleaf.service.ThymeleafService;

/**
 * @author Sunk_Sky
 *
 *         2014年12月1日上午9:49:34
 */
@Service("InitService")
public class InitServiceImpl implements InitService
{
	@Autowired
	@Qualifier("HtmlMapperDao")
	private HtmlMapperDao htmlDao;

	@Autowired
	@Qualifier("TypeService")
	private TypeService typeService;

	@Autowired
	@Qualifier("ProductService")
	private ProductService productService;

	@Autowired
	@Qualifier("CaseService")
	private CaseService caseService;
	
	@Autowired
	@Qualifier("RelatedProductService")
	private RelatedProductService relatedService;

	@Autowired
	@Qualifier("ThymeleafService")
	private ThymeleafService thymeleafService;

	@Autowired
	@Qualifier("SearchService")
	private SearchService searchService;

	@Autowired
	@Qualifier("RecruitmentService")
	private RecruitmentService recruitmentService;
	
	@Autowired
	@Qualifier("NewsService")
	private NewsService newsService;

	@Value("${thymeleaf.hostIp}")
	private String hostIp;

	@Override
	public boolean initHK()
	{
		String language = Constants.LANG_ZH_HK;

		return this.init(language);
	}

	@Override
	public boolean initEN()
	{
		String language = Constants.LANG_EN;

		return this.init(language);
	}

	@Override
	public boolean initCN()
	{
		String language = Constants.LANG_ZH_CN;

		return this.init(language);
	}

	@Override
	public boolean initAll()
	{
//		this.hostIp = this.getIP();

		this.searchService.addIndex("http://www.baidu.com");

		this.searchService.deleteAll();

		this.initHK();

		this.initCN();

		this.initEN();

		return true;
	}

	private boolean init(String language)
	{
		Map<String, Object> data = new HashMap<String, Object>();

		data.put("typeList", this.getTypeList(language));

		data.put("hostIp", this.hostIp);
		
//		data.put("hostIp", "");

		data.put("recuitmentList", this.getRecruitmentList(language));
		
		data.put("commentsList", this.getNewsList("market_comments"));
		
		data.put("reportsList", this.getNewsList("market_reports"));
		// 先处理type种类
		List<HtmlMapperModel> htmlList = this.getMapper(language, 0);

		for (HtmlMapperModel temp : htmlList)
		{
			String url = thymeleafService.transferType(temp.getKeyword(), data);

			if (!(url == null || "".equalsIgnoreCase(url)))
			{
				
				if(temp.getKeyword().contains("search"))
				{
					break;
				}
				this.searchService.addIndex(url);
			}
		}

		// 处理information种类，目前information种类只有product一种，直接进行处理，不使用接口
		TypeModel model = new TypeModel();

		model.setType(TypeModelType.button);

		model.setLang(language);

		List<TypeModel> list = typeService.query(model);

		for (TypeModel te : list)
		{
			data.put("product", te);

			{
				CaseModel caseCondition = new CaseModel();

				caseCondition.setLang(te.getLang());

				caseCondition.setType_id(te.getId());

				CaseModel caseResult = caseService.queryOne(caseCondition);

				data.put("case", caseResult);
			}

			{
				ProductModel productCondition = new ProductModel();

				productCondition.setLang(te.getLang());

				productCondition.setType_id(te.getId());

				List<ProductModel> productList = productService
						.query(productCondition);

				data.put("productList", productList);
			}
			{
				RelatedProductModel relatedCondition = new RelatedProductModel();
				
				relatedCondition.setType_id(te.getId());
				
				relatedCondition.setLang(te.getLang());
				
				List<RelatedProductModel> relatedList = this.relatedService.query(relatedCondition);
				
				data.put("relatedList", relatedList);
			}
			

			data.put("hostIp", this.hostIp);
//			
//			data.put("hostIp", "");

			String url = this.thymeleafService.transferInformation(language
					+ ".product", data, te.getCode());

			System.out.println(url);
			if (!(url == null || "".equalsIgnoreCase(url)))
			{
				this.searchService.addIndex(url);
			}
		}
		return true;
	}

	/**
	 * 
	 * @param lang
	 * @param type
	 *            type ==0 :type;type!=0:information
	 * @return
	 */
	private List<HtmlMapperModel> getMapper(String lang, int type)
	{
		HtmlMapperModel condition = new HtmlMapperModel();

		condition.setLang(lang);

		if (type == 0)
		{
			condition.setType(HtmlType.type);
		} else
		{
			condition.setType(HtmlType.information);
		}

		return this.htmlDao.query(condition);

	}

	private List<TypeModel> getTypeList(String lang)
	{
		TypeModel condition = new TypeModel();

		condition.setId(1);

		condition.setLang(lang);

		List<TypeModel> typeList = typeService.getChildren(condition);

		for (TypeModel temp : typeList)
		{
			List<TypeModel> list = typeService.getChildren(temp);

			temp.setChildren_list(list);
		}

		return typeList;
	}

	private List<RecruitmentModel> getRecruitmentList(String lang)
	{
		RecruitmentModel condition = new RecruitmentModel();

		condition.setLang(lang);

		return this.recruitmentService.query(condition);
	}

	
	private List<InformationModel> getNewsList(String code)
	{
		NewsModel condition = new NewsModel();
		
		condition.setCode(code);
		
		return this.newsService.query(condition);
	}
	
	
	
	private String getIP()
	{
		String result = "";
		try
		{
			InetAddress netAddress = InetAddress.getLocalHost();

			result = netAddress.getHostAddress();

		} catch (UnknownHostException e)
		{
			
			e.printStackTrace();
		}

		return "http://" + result + "/";
	}
	
}
